//
//  HJSubmitOderViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSubmitOderViewController.h"
#import "HJBuyView.h"
#import "HJBuyBottom.h"
#import "HJGoodsAdressController.h"
#import "HJPayViewController.h"
#import "HJHomeModel.h"
#import "HJCodeView.h"

@interface HJSubmitOderViewController ()
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)HJBuyView * headerView;
@property(nonatomic,strong)HJBuyBottom * buyBottom;
@property(nonatomic,assign) NSInteger currentType;
@property(nonatomic,strong) HJOderModel * oderModel;
@property(nonatomic,strong) NSString * days;
@property(nonatomic,assign)BOOL select_Btn;

@end

@implementation HJSubmitOderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [mNotificationCenter addObserver:self selector:@selector(consigneeAdresBtn:) name:@"consigneeAdresBtn" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(buyBottomBtn:) name:@"buyBottomBtn" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(codeNotification:) name:@"codeNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(timeNotification:) name:@"timeNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(priceTextFieldNotification:) name:@"priceTextFieldNotification" object:nil];
    [self configScroll];
    [self loadDataWithWork];
    self.select_Btn = NO;
    self.days = @"3";
}


-(void)loadDataWithWork {
    NSMutableDictionary * params =[NSMutableDictionary dictionary];
    params[@"shoes_id"] =self.shoes_id;
    params[@"size"] =self.shoes_size;
    params[@"type"] =self.type;
    [TZHttpTool post:ConfirmAPI params:params success:^(NSDictionary *result) {
        self.oderModel = [[HJOderModel alloc]init];
        self.oderModel = [HJOderModel mj_objectWithKeyValues:result[@"data"]];
        self.headerView.shoesModel = self.oderModel;
        self.buyBottom.model = self.oderModel;
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

#pragma mark -- 收货地址
-(void)consigneeAdresBtn:(NSNotification *)not {
    if ([HJUserManager isLoginView]) {
        return;
    }
    HJGoodsAdressController * shippingVC = [[HJGoodsAdressController alloc]init];
    shippingVC.navTitle = @"地址选择";
    shippingVC.enditBool = NO;
    WeakSelf
    shippingVC.Blok = ^(NSString *name, NSString *address, NSString *mobile, NSString * adress_id) {
        weakSelf.buyBottom.AdressL.hidden = YES;
        weakSelf.buyBottom.consignee.text = [NSString stringWithFormat:@"收货人：%@",name];;
        weakSelf.buyBottom.adress.text = [NSString stringWithFormat:@"地   址：%@",address];
        weakSelf.buyBottom.phoneNo.text = [NSString stringWithFormat:@"手机号：%@",mobile];
        weakSelf.buyBottom.address_id = adress_id;
    };
    [self.navigationController pushViewController:shippingVC animated:YES];
}

-(void)configScroll {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, mScreenHeight)];
    self.scrollView.bounces = YES;
    self.scrollView.clipsToBounds = YES;
    self.scrollView.contentSize = CGSizeMake(mScreenWidth, 305 +402+200);
    self.scrollView.backgroundColor = HJColorRGB(246);
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:_scrollView];
    
    [self configHeader];
    [self configBuyBottom:self.currentPush.integerValue];
}
-(void)configHeader {
    MJWeakSelf;
    self.headerView = [[HJBuyView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, 303)];
    self.headerView.size = self.shoes_size;
    self.headerView.cost = self.cost;
    self.headerView.currentTag =[self.currentPush integerValue];
    self.headerView.block = ^(NSInteger tag, UIButton *btn) {
        weakSelf.currentPush = @(tag);
        weakSelf.currentType = tag;
        [weakSelf.buyBottom removeFromSuperview];
        [weakSelf configBuyBottom:tag];
    };
    [self.scrollView addSubview:self.headerView];
}
-(void)configBuyBottom:(NSInteger)tag {
    self.buyBottom = [[HJBuyBottom alloc]init];
    if (tag==100) {
        self.buyBottom.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), mScreenWidth, 295);
    }else {
        self.buyBottom.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), mScreenWidth, 405);
    }
    self.buyBottom.tag = tag;
    self.buyBottom.model = self.oderModel;
    [self.scrollView addSubview:self.buyBottom];
}

-(void)buyBottomBtn:(NSNotification *)not {
    if (!self.select_Btn) {
        if ([HJUserManager isLoginView]) {
            return;
        }
        if (!self.buyBottom.address_id) {
            [self showErrorHUDWithError:@"请新增地址"];return;
        }
        if (!self.oderModel.sell.shoes_cost) {
             [self showErrorHUDWithError:@"请输入价格"];return;
        }
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        if ([self.currentPush isEqual:@100]) {
            params[@"type"] = @"3";
        }else {
            params[@"type"] = @"4";
            params[@"days"] = self.days;
        }
        params[@"shoes_id"] = self.shoes_id;
        params[@"shoes_size"]= self.shoes_size;
        params[@"shoes_cost"] = self.oderModel.sell.shoes_cost;
        params[@"address_id"] = self.buyBottom.address_id;
        [self loadOrderAdd:params];
        self.select_Btn = YES;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.select_Btn = NO;
        });
    }
}
#pragma mark -- 求购提交订单
-(void)loadOrderAdd:(NSMutableDictionary *)params {
    [TZHttpTool post:OrderAddAPI params:params success:^(NSDictionary *result) {
        [self showHint:@"提交成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            HJPayViewController * payVC = [[HJPayViewController alloc]init];
            payVC.navTitle = @"支付中心";
            payVC.pay = result[@"data"][@"pay"];
            if ([params[@"type"] isEqualToString:@"3"]) {
               payVC.order_sn = result[@"data"][@"order_sn"];
            }else {
                payVC.order_sn = result[@"data"][@"want_buy_id"];
            }
            payVC.order_type = params[@"type"];
            payVC.order_id =result[@"data"][@"order_id"];
            [self.navigationController pushViewController:payVC animated:YES];
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

-(void)codeNotification:(NSNotification *)not {
    self.needPopCode = YES;
    self.codeView.hidden =NO;
    self.backView.hidden = NO;
    self.codeView.size = self.shoes_size;
    self.codeView.shoes_id = self.shoes_id.numberValue;
    self.codeView.selecte_cost = @1;
    if ([self.currentPush isEqual:@100]) {
         self.codeView.type = cost_is_nil;
    }else {
        self.codeView.type = cost_is_yes;
    }
   
    [self.codeView loadSizeWorkWithShoesid:self.shoes_id.numberValue size:self.shoes_size];
}
- (void)CodeViewDidClickOKButton:(NSString *)size shoes_id:(NSNumber *)shoes_id cost:(NSString *)cost type:(NSNumber *)type {
    self.needPopCode = NO;
    self.codeView.hidden =YES;
    self.backView.hidden =YES;
    [self.headerView.codeBtn setTitle:[NSString stringWithFormat:@"%@码",size] forState:UIControlStateNormal];
    self.oderModel.sell.shoes_cost = cost;
    self.headerView.consultPrice.text = [NSString stringWithFormat:@"参考价：%@",cost];
    self.shoes_id = shoes_id.stringValue;
    self.shoes_size = size;
    
    if ([type isEqual:@1]) {
        self.headerView.priceTextField.text = @"";
        [self.buyBottom.buyBottomBtn setTitle:@"立刻求购" forState:UIControlStateNormal];
    }else {
        self.headerView.priceTextField.text = [NSString stringWithFormat:@"￥%@",cost];
        [self.buyBottom.buyBottomBtn setTitle:[NSString stringWithFormat:@"去付款￥%@",cost] forState:UIControlStateNormal];
    }

}
-(void)priceTextFieldNotification:(NSNotification *)not {
    self.oderModel.sell.shoes_cost = [not.userInfo[@"text"] stringByReplacingOccurrencesOfString:@"￥" withString:@""];
}

-(void)timeNotification:(NSNotification *)not {
    self.days =not.userInfo[@"days"];
}
@end
